package struts;

import hibernate.PageInfo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.support.WebApplicationContextUtils;

import service.BaseServiceI;

//import com.opensymphony.xwork2.ActionSupport;

/**
 * @author zhangzhw 
 * 所有Action的基类，默认处理用户进程及错误
 */
public class BaseAction //extends ActionSupport 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2537910421815160605L;
	protected final String SUCCESS = "success";	
	ApplicationContext ctx;
	private BaseServiceI baseService;

	public BaseServiceI getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseServiceI baseService) {
		this.baseService = baseService;
	}

	
	// 所有action 入口 处理 系统 登录和错误的问题
	public String execute() {
		// 处理系统问题
		try {
			// 未登录转入登录页面
			if (!login())
				if (!getUrl().equals("/jcmis/action/login.action"))
					return "login";

			// 执行请求操作
			return exe();
		} catch (Exception e) {
			e.printStackTrace();
			// 返回错误页面
			return "error";
		}
	}

	// 取得request对象
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	// 取得response对象
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	// 取得session对象
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	// 判断登录
	public boolean login() {
		if (getSession().getAttribute("name") == null)
			return false;
		return true;
	}

	// 取得 applicationContext对象
	public ApplicationContext getCtx() {
		return WebApplicationContextUtils
				.getRequiredWebApplicationContext(ServletActionContext
						.getServletContext());

	}

	// 取得bean
	public Object getBean(String beanName) {
		return ctx.getBean(beanName);
	}

	public String getUrl() {
		String url = getRequest().getRequestURI();
		return url;
	}

	// 封装后的执行类 待覆盖
	public String exe() {
		return "success";
	}

	// 保存
	public void save(Object obj) {
		getBaseService().save(obj);
	}

	// 删除
	public void delete(Object obj) {
		getBaseService().delete(obj);
	}

	// 更新
	public void update(Object obj) {
		getBaseService().update(obj);
	}

	// 自动保存或更新
	public void uporsave(Object obj) {
		getBaseService().uporsave(obj);
	}

	// 批量保存
	@SuppressWarnings("unchecked")
	@Transactional(rollbackForClassName="Exception")
	public boolean save(List list) {
		return getBaseService().save(list);
	}

	// 批量自动更新
	@SuppressWarnings("unchecked")	
	public boolean uporsave(List list) {
		return getBaseService().saveorupdate(list);
	}

	// 查询
	@SuppressWarnings("unchecked")
	public List findAll(String queryString) {
		return getBaseService().findAll(queryString);
	}

	// 分页查询
	public PageInfo findAll(String queryString, int pageNo, int pageCount) {
		return getBaseService().findAll(queryString, pageNo, pageCount);
	}

	// 根据主键查询
	public Object findByID(String clazz, Long id) {
		return getBaseService().findByID(clazz, id);
	}

	@SuppressWarnings("unchecked")
	public List findByExample(Object obj) {
		return getBaseService().findByExample(obj);
	}

	public static int str2int(String string) {
		if (string == null)
			return 0;
		else
			return Integer.valueOf(string);
	}

}
